<template>
    <div class="register">
        <div class="box">
            <div class="zhuce">
                <h1>注册</h1>
                <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
                  <el-form-item label="用户名" prop="user_name">
                        <el-input v-model.trim="ruleForm.user_name" class="input"></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="pass">
                        <el-input type="password" v-model.trim="ruleForm.pass" autocomplete="off" class="input"></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="checkPass">
                        <el-input type="password" v-model.trim="ruleForm.checkPass" autocomplete="off" class="input"></el-input>
                    </el-form-item>
                    <span id="login">已有账号？点击<a @click="login">登录</a></span>
                    <el-form-item class="but">
                        <!-- <el-button @click="">提交</el-button> -->
                        <el-button  type="primary" :plain="true" @click="submitForm(),open2()">提交</el-button>
                        <el-button @click="Reset">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
import {req_register} from '@/utils/registerLogin.js';
import { Message } from 'element-ui';
    export default {
    data() {
      var checkUser_name = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('用户名不能为空'));
        }
          callback();
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
          // open2();
        }
      };
      return {
        ruleForm: {
          pass: '',
          checkPass: '',
          user_name: ''
        },
        rules: {
          pass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          user_name: [
            { validator: checkUser_name, trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      async submitForm() {
        console.log(this.ruleForm.pass,this.ruleForm.checkPass,this.ruleForm.user_name)
        let result = await req_register({
          method:'post',
          url:'/register',
          params:{
            user_name:this.ruleForm.user_name,
            password:this.ruleForm.pass
          },
          headerType:'form',
        });
        console.log(result);
        this.ruleForm.user_name = '';
        this.ruleForm.pass = '';
        this.ruleForm.checkPass = '';
      },
      Reset() {
        this.ruleForm.pass = '';
        this.ruleForm.checkPass = '';
        this.ruleForm.user_name = '';
      },
      login(){
        this.$router.replace('/login');
      },
      open2() {
        Message({
          message: '注册成功',
          type: 'success',
          
        });
      }
    }
  }
</script>

<style scoped>
.register{
  width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    background-image: linear-gradient(to left, #fd79a8, #a29bfe);
}
.box{
  width: 400px;
    height: 500px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: white;
    border-radius: 15px;
}
.zhuce{
  width: 400px;
    height: 400px;
}
.input{
    width: 80%;
}
h1{
    font: 900 40px '';
    text-align: center;
    margin-bottom: 60px;
}
.but{
  display: flex;
  justify-content: center;
  align-items: center;
  margin-left: -80px;
}
#login{
    display: block;
    font-size:12px;
    margin-bottom:20px;
    margin-left: 10px;
    color:#606266;
}
a{
    color: #a29bfe;
    cursor:pointer;
}
a:hover{
    color: #fd79a8;
    cursor:pointer;
}
</style>